// pages/week/week.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // sch_listData: [],
    // dateArray: [],
    // msg: '数据',
    classroom:'',
    tabs: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    itemWidth: 0,
    windowWidth: 0,
    tabIndex: 0,
    sliderLeft: 0,
    sliderOffset: 0,
    sliderOffsets: [],
    items:[],
    date:''
  },
  showText: function(){
    console.log(123)
  },

  showDay: function(){
    wx.navigateTo({
      url: '../day/day?room='+this.classroom,
    })
  },
  applyFor: function(){
    let show_day = new Array('日','一','二','三','四','五','六')
    let date = new Date();
    date.setDate(date.getDate());
    let day = date.getDay();
    if(day==0) day=7;
    // day=5;
    let query=this.data.tabIndex+1;
    let offset=(query-day)>=0?(query-day):(7-day+query);
    var current = new Date().getTime(); // 拿到当前的时间戳
    var future = current + offset * 24 * 3600 * 1000; // 计算出所选星期的时间戳
    future = new Date(future); // 转成时间对象
    var futureMonth = future.getMonth() + 1; // 获取所选星期的月份
    var futureDate  = future.getDate();      // 获取所选星期的日期
    futureDate = futureDate < 10 ? '0' + futureDate : futureDate.toString(); // 如果日期小于10则补个0
    var futuerYear = new Date().getFullYear();
    this.setData({
      date:futuerYear+'-'+futureMonth+"-"+futureDate
    })
    // var date = futuerYear+'-'+futureMonth+"-"+futureDate
    // console.log(futureMonth + '/' + futureDate); // 打印日期
    wx.navigateTo({
      url: '../apply/apply?room='+this.classroom+'&date='+this.data.date,
    })
  },
   // 处理点击tab
   onTabClick(e) {
    let id = e.currentTarget.id;
    // 取新的偏移，完成transition和transform的参数
    let newOffset = this.data.sliderOffsets[id];
    this.setData({
      tabIndex: id,
      sliderOffset: newOffset
    })
  },

  // 处理swiper改变
  swiperChange(e) {
    this.setData({
      sliderOffset: this.data.sliderOffsets[e.detail.current],
      tabIndex: e.detail.current,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    wx.setNavigationBarTitle({
      title: options.room+'教室使用情况'
    })
    this.classroom=options.room
    // console.log(this.classroom)
    wx.request({
      url:'https://yali.geekwhite.cn:3000/week', 
      header:{'Access-Control-Allow-Origin':'*'},
      header:{'Access-Control-Allow-Methods':'GET,POST'},
      data:{
        classroom:this.classroom,
        // classroom:'C204'
        // items:'123'
      },
      method:'post',
      success:res=>{
        // console.log(res)
        res.data.map((item)=>{
          // console.log(item)
          item.map((items)=>{
            let sstr=items.Start+''
            let estr=items.Endtime+''
            if(sstr.length==3){
              items.Start='0'+sstr.substring(0,sstr.length-2)+':'+sstr.substring(1,sstr.length)
            }
            else{
              items.Start=sstr.substring(0,sstr.length-2)+':'+sstr.substring(2,sstr.length)

            }
            if(estr.length==3){
              items.Endtime='0'+estr.substring(0,estr.length-2)+':'+estr.substring(1,estr.length)
            }
            else{
              items.Endtime=estr.substring(0,estr.length-2)+':'+estr.substring(2,estr.length)

            }
          })

        })
        this.setData({
          items:res.data
        })
        // console.log(items)
      }
    })
    // console.log(this.data.items[0])
    let that = this;
    // 计算
    wx.getSystemInfo({
      success: function(res) {
        // 每个item应占的宽度向上取整，限tab栏不会滑动的情况。
        let windowWidth = res.windowWidth;
        let itemWidth = Math.ceil(windowWidth / that.data.tabs.length);
        // 初始化每个项目的偏移量，存入数组
        let tempArr = [];
        for (let i in that.data.tabs) {
          tempArr.push(itemWidth * i);
        }

        // 32是两个字体（16px）的宽度。tab中字数不同的话需要调整...
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - 32) / 2,
          sliderOffsets: tempArr,
          sliderOffset: 0,
          itemWidth: itemWidth,
          windowWidth: windowWidth
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(this.data.items)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})